package mk.jug.academy.service;

import mk.jug.academy.dao.EventDAO;
import mk.jug.academy.model.Event;
import mk.jug.academy.model.Page;
import mk.jug.academy.preconditions.PreconditionExceptions;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


/**
 * Contains the business logic implementation of {@link EventService}.
 *
 * @author Ice.Penov
 */
@Service
public class EventServiceImpl implements EventService {
	
	@Autowired private EventDAO eventDAO;
	
	@Override
	public void createEvent(Event event) {
		eventDAO.save(event);
	}

	@Override
	public void removeEvent(Event event) {
		eventDAO.delete(event);
	}

	@Override
	public void saveChanges(Event event) {
		Assert.notNull(event.getId(), PreconditionExceptions.ID_IS_NULL);
		eventDAO.save(event);
	}
	
	@Override
	public Event load(Long id) {
		Assert.notNull(id, PreconditionExceptions.ID_IS_NULL);
		return eventDAO.find(Event.class, id);
	}

	@Override
	public Page<Event> list(int pageNumber, int itemsPerPage) {
		return eventDAO.findLatest(Utils.calculateStart(pageNumber, itemsPerPage), itemsPerPage);
	}
	
}